package com.lh.controller;

import com.lh.service.GoodsService;
import com.lh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理员和用户共用一张表，通过角色权限控制
 * 为了方便代码的阅读性，将管理员和用户的控制层分开
 * 管理员：AdminController
 * 用户：UserController
 *
 * @Author: LuoHang
 * @Date: 2021/2/5 16:45
 * @Description
 */
@Controller
public class AdminController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;

    /**
     * 管理员主页
     * 登录和验证登录与普通用户共用，详情见UserController
     *
     * @return 视图模型
     */
    @RequestMapping("/adminMenu")
    public ModelAndView adminIndex() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/index");
        return modelAndView;
    }

    /**
     * 管理员用户登录欢迎页，系统后台桌面
     *
     * @return 视图模型
     */
    @RequestMapping("/welcome")
    public ModelAndView welcome() {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = new HashMap<>(6);
        map.put("clothingGoodsNum", goodsService.getGoodsCategoryCount(1));
        map.put("beautyGoodsNum", goodsService.getGoodsCategoryCount(2));
        map.put("bookGoodsNum", goodsService.getGoodsCategoryCount(3));
        map.put("eleGoodsNum", goodsService.getGoodsCategoryCount(4));
        map.put("toyGoodsNum", goodsService.getGoodsCategoryCount(5));
        map.put("foodGoodsNum", goodsService.getGoodsCategoryCount(6));
        // tuesday wednesday thursday friday saturday sunday
        modelAndView.addObject("incrementNum",getIncrementMap());
        modelAndView.addObject("adminCount",userService.getRoleUserCount(1));
        modelAndView.addObject("userCount",userService.getRoleUserCount(2));
        modelAndView.addObject("merchantCount",userService.getRoleUserCount(3));
        modelAndView.addObject("categoryGoodsNum", map);
        modelAndView.setViewName("admin/welcome");
        return modelAndView;
    }

    /**
     * 后台管理员账号切换
     *
     * @return 视图模型
     */
    @RequestMapping("/changeAccount")
    public ModelAndView changeAccount() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/changeAccount");
        return modelAndView;
    }

    /**
     * 后台管理员列表页面
     *
     * @return 视图模型
     */
    @RequestMapping("/adminList")
    public ModelAndView adminList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/admin_list");
        return modelAndView;
    }

    /**
     * 交易成交量量图
     *
     * @return 视图模型
     */
    @RequestMapping("/transactionHtml")
    public ModelAndView transactionHtml(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/transaction");
        return modelAndView;
    }

    /**
     * 用户统计图页面
     * 用户角色分类对应的人数
     * 最近一周每日新增用户量
     *
     * @return 视图模型
     */
    @RequestMapping("/userCountHtml")
    public ModelAndView userCountHtml(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("adminCount",userService.getRoleUserCount(1));
        modelAndView.addObject("userCount",userService.getRoleUserCount(2));
        modelAndView.addObject("merchantCount",userService.getRoleUserCount(3));
        modelAndView.addObject("incrementNum",getIncrementMap());
        modelAndView.setViewName("admin/user_count");
        return modelAndView;
    }

    /**
     * 方法：每周用户日增长量
     *
     * @return 每周用户日增长量的数据
     */
    private Map<String,Object> getIncrementMap(){
        Calendar today = Calendar.getInstance();
        Map<String,Object> incrementNum = new HashMap<>(7);
        int i = today.get(Calendar.DAY_OF_WEEK) - 1;
        Date curDate = new Date(System.currentTimeMillis());
        if (i==1){
            incrementNum.put("monday",userService.getRegisterCountByDay(curDate));
            incrementNum.put("tuesday",0);
            incrementNum.put("wednesday",0);
            incrementNum.put("thursday",0);
            incrementNum.put("friday",0);
            incrementNum.put("saturday",0);
            incrementNum.put("sunday",0);
        }else if (i==2){
            incrementNum.put("monday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-24*3600000)));
            incrementNum.put("tuesday",userService.getRegisterCountByDay(curDate));
            incrementNum.put("wednesday",0);
            incrementNum.put("thursday",0);
            incrementNum.put("friday",0);
            incrementNum.put("saturday",0);
            incrementNum.put("sunday",0);
        }else if (i==3){
            incrementNum.put("monday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-2*24*3600000)));
            incrementNum.put("tuesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-24*3600000)));
            incrementNum.put("wednesday",userService.getRegisterCountByDay(curDate));
            incrementNum.put("thursday",0);
            incrementNum.put("friday",0);
            incrementNum.put("saturday",0);
            incrementNum.put("sunday",0);
        }else if (i==4){
            incrementNum.put("monday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-3*24*3600000)));
            incrementNum.put("tuesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-2*24*3600000)));
            incrementNum.put("wednesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-24*3600000)));
            incrementNum.put("thursday",userService.getRegisterCountByDay(curDate));
            incrementNum.put("friday",0);
            incrementNum.put("saturday",0);
            incrementNum.put("sunday",0);
        }else if (i==5){
            incrementNum.put("monday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-4*24*3600000)));
            incrementNum.put("tuesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-3*24*3600000)));
            incrementNum.put("wednesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-2*24*3600000)));
            incrementNum.put("thursday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-24*3600000)));
            incrementNum.put("friday",userService.getRegisterCountByDay(curDate));
            incrementNum.put("saturday",0);
            incrementNum.put("sunday",0);
        }else if (i==6){
            incrementNum.put("monday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-5*24*3600000)));
            incrementNum.put("tuesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-4*24*3600000)));
            incrementNum.put("wednesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-3*24*3600000)));
            incrementNum.put("thursday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-2*24*3600000)));
            incrementNum.put("friday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-24*3600000)));
            incrementNum.put("saturday",userService.getRegisterCountByDay(curDate));
            incrementNum.put("sunday",0);
        }else if (i==7){
            incrementNum.put("monday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-6*24*3600000)));
            incrementNum.put("tuesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-5*24*3600000)));
            incrementNum.put("wednesday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-4*24*3600000)));
            incrementNum.put("thursday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-3*24*3600000)));
            incrementNum.put("friday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-2*24*3600000)));
            incrementNum.put("saturday",userService.getRegisterCountByDay(new Date(System.currentTimeMillis()-24*3600000)));
            incrementNum.put("sunday",userService.getRegisterCountByDay(curDate));
        }
        return incrementNum;
    }
}
